<script setup lang="ts">
import router from '@/router'
// 导入图片资源
import type { ProjectCardProps } from '../types/ProjectCardProps'
import { onMounted, ref, watch } from 'vue';
const props = defineProps<{ app: ProjectCardProps, }>()

const app = ref(props.app)
const likes = ref(0);
const collected = ref(false);
const liked = ref(false);
const views = ref(0);
// 监听 app 的变化
watch(() => props.app, (newApp) => {
  // console.log('New app data received:', newApp);
  app.value = newApp; // 更新 app 数据
});

// 获取应用数据
onMounted(() => {
  // console.log('Initial App Data:', app.value);
  likes.value = Number(app.value.likes || 0);
  views.value = Number(app.value.views || 0);
  collected.value = false;
  liked.value = false;
});

const handleMoreInfo = () => {
  // console.log('Navigating to project details with projectId:', props.app.projectId)
  if (props.app.projectId)
    router.push({ name: 'projectDetails', params: { projectId: props.app.projectId } })
}
</script>

<template>
  <div class="data-item">
    <div class="card-top">
      <img :src="app.image" :alt="app.title" class="data-image">

      <div class="data-details">
        <div class="card-top-left">
          <h2>{{ app.title }}</h2>
        </div>

        <div class="tags">
          <span v-for="(tag, index) in app.tags" :key="index" class="tag">{{ tag }}</span>
          <span v-if="app.tags.length > 3" class="tag">...</span>
        </div>

        <div class="description">
          <span v-if="app.description === ''" />
          <ul v-else>
            {{ app.description }}
          </ul>
        </div>
      </div>
    </div>
    <div class="card-bottom">
      <button class="detail-button" @click="handleMoreInfo">
        查看详情
      </button>
    </div>
  </div>
</template>

<style scoped>
.data-item {
    border-radius: 1rem;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    padding: 0.5rem;
    transition: transform 0.2s; /* 添加一个轻微的缩放效果 */
    cursor: default; /* 鼠标悬停在卡片上时不变 */
}
.card-top {
    display: flex;
    padding: 0.5rem;
}

.data-image {
    width: 9rem;
    height: 9rem;
    object-fit: cover;
    border-radius: 0.5rem;
    margin-right: 0.5rem;
}

.data-details {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex-grow: 1;
}

.card-top-left {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.data-details h2 {
    margin-right: -0.9375rem; /* -15px */
    font-size: 1.25rem; /* 20px */
}

.favorites {
    flex-shrink: 0;
    padding: 0.1875rem 0.375rem; /* 3px 6px */
    margin-left: 0.625rem; /* 10px */
}

.tags {
    display: flex;
    gap: 0.625rem; /* 10px */
    margin: 0.625rem 0; /* 10px 0 */
}

.tag {
    background: #fff;
    color: black;
    border: 0.0625rem solid #ccc; /* 1px */
    border-radius: 0.25rem; /* 4px */
    padding: 0.25rem 0.3125rem; /* 4px 5px */
    font-size: 1rem; /* 16px */
}

.description {
    margin: 0;
    font-size: 1.125rem; /* 18px */
    color: #555;
    display: -webkit-box;
    -webkit-line-clamp: 2; /* 限制文本行数为三行 */
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .card-bottom {
    display: flex;
    justify-content: space-between;
}

.meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.625rem; /* 10px */
    font-size: 0.9rem; /* 0.8em */
    color: #f04c4c;
}

.meta span {
    display: flex;
    align-items: center;
    gap: 0.3125rem; /* 5px */
    margin-right: 2.5rem; /* 40px */
}

.meta img {
    width: 1.5625rem; /* 25px */
    height: 1.5625rem; /* 25px */
}

.actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.625rem; /* 10px */
    width: 100%;
}

.favorites {
    display: flex;
    align-items: center;
    font-size: 0.7875rem; /* 0.9em */
    color: #333;
    border: 0.0625rem solid #ddd; /* 1px */
    border-radius: 0.9375rem; /* 15px */
    padding: 0.1875rem 0.375rem; /* 3px 6px */
    margin-right: -0.3125rem; /* -5px */
}

.favorites img {
    width: 0.9375rem; /* 15px */
    height: 0.9375rem; /* 15px */
    margin-right: 0.125rem; /* 2px */
    margin-top: -10%;
    vertical-align: middle;
}

.detail-button {
    background-color: #e6f0ff;
    color: #2669f3;
    border: none;
    font-size: 15px; /* 18px */
    padding: 8px 18px; /* 10px 20px */
    border-radius: 3px; /* 4px */
    margin-left: auto;
    cursor: pointer; /* 鼠标悬停在按钮上时变成小手 */
}

.detail-button:hover {
    background-color: #3867ea;
    color: #fff;
}
</style>
